package httpHandler

import (
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	sessionHandler "gitee.com/zaiqiang231/go-base-session/app/http/handler"
	"gitee.com/zaiqiang231/go-base-session/app/rpc/proto"
	errCode "gitee.com/zaiqiang231/zcamp-service-user/app/constants"
	remoteRpc "gitee.com/zaiqiang231/zcamp-service-user/app/rpc/remote"
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	KEY_AUTH_UID = "auth_uid"
)

// AuthCheckFunc 鉴权
func AuthCheckFunc(c *gin.Context) {
	atom := sessionHandler.GetAtom(c)
	response, err := remoteRpc.GetSessionRpc().SessionRpcServiceClient.SessionCheck(c, &proto.SessionCheckRequest{
		Uid: atom.Uid,
		Sid: atom.Sid,
	})
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	if !response.IsValid {
		baseHttp.WriteResponse(c, http.StatusBadRequest, errCode.SessionErr, nil)
		return
	}

	c.Set(KEY_AUTH_UID, atom.Uid)
	c.Next()
}
